package com.tengroup.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tengroup.dao.PracticeDao;
import com.tengroup.dao.PracticeepDao;
import com.tengroup.dao.QuestionMapper;
import com.tengroup.entity.PracticeEntity;
import com.tengroup.entity.PracticeepEntity;
import com.tengroup.entity.Question;
import com.tengroup.vo.PracticeVo;
import com.tengroup.service.QServicempl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service("practice")
public class PracticeServiceImpl extends ServiceImpl<PracticeepDao, PracticeepEntity> implements PracticeService {
    @Autowired
    private PracticeDao practiceDao;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private PracticeepDao practiceepDao;
    @Autowired
    private QServicempl qservice;

    @Override
    public List<PracticeepEntity> generatePractice(Long subjectId, Integer count) {
        List<Question> questions = selectQuestionsBySubject(subjectId);
        PracticeEntity practice = createPractice(subjectId);
        List<PracticeepEntity> practiceQuestions = savePracticeQuestions(practice, questions, count);
        return practiceQuestions;
    }

    private List<Question> selectQuestionsBySubject(Long subjectId) {
        List<Question> questions = questionMapper.findBySubjectId(Math.toIntExact(subjectId));
        Collections.shuffle(questions); // 打乱题目顺序
        return questions;
    }

    private PracticeEntity createPractice(Long subjectId) {
        PracticeEntity practice = new PracticeEntity();
        practice.setSubjectId(subjectId);
        practiceDao.insert(practice);
        return practice;
    }

    private List<PracticeepEntity> savePracticeQuestions(PracticeEntity practice, List<Question> questions,
                                       Integer count) {
        List<PracticeepEntity> practiceQuestions = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            PracticeepEntity practiceep = new PracticeepEntity();
            practiceep.setQuestionId(questions.get(i).getId());
            practiceep.setContent(questions.get(i).getStem());
            practiceep.setOptions(questions.get(i).getOptions());
            practiceep.setQuestionType(questions.get(i).getQuestionType());
            practiceep.setPracticeId(practice.getId());
            practiceep.setSubjectId(practice.getSubjectId());
            practiceepDao.insert(practiceep);
            practiceQuestions.add(practiceep);
        }
        return practiceQuestions;
    }

    @Override
    public List<PracticeepEntity> findByPracticeId(Long practiceId) {
        return practiceepDao.findByPracticeId(practiceId);
    }

    @Override
    public List<PracticeepEntity> getAnswerAndJudge(PracticeVo practiceVo) {
        List<PracticeepEntity> practice = practiceepDao.findByPracticeId(practiceVo.getPracticeId());
        List<String> answers = practiceVo.getAnswers();

        // 检查 answers 是否为空
        if (answers == null || answers.isEmpty()) {
            throw new IllegalArgumentException("Answers list is empty or null");
        }

        // 检查 practice 和 answers 的长度是否一致
        if (practice.size() != answers.size()) {
            throw new IllegalArgumentException("Number of questions and answers do not match");
        }

        // 更新答案
        for (int i = 0; i < practice.size(); i++) {
            practice.get(i).setUserAnswer(answers.get(i));
            practiceepDao.updateById(practice.get(i));
        }

        for (PracticeepEntity practiceepEntity : practice) {
            Question q = qservice.getById(practiceepEntity.getQuestionId());
            if (q == null) {
                throw new IllegalArgumentException("Question with ID " + practiceepEntity.getQuestionId() + " not found");
            }
            else{
                practiceepEntity.setTrueAnswer(q.getAnswer());
            if (q.getAnswer().equals(practiceepEntity.getUserAnswer())) {
                practiceepEntity.setJudge(1);
                practiceepEntity.setAnalysis(q.getAnalysis());
            }
            else{
                practiceepEntity.setJudge(0);
                practiceepEntity.setAnalysis(q.getAnalysis());
            }
            baseMapper.updateById(practiceepEntity);}
        }
        return practice;
    }
}